package net.skyedu.service.myattend;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.skyedu.bean.Appeal;
import net.skyedu.bean.Attendance;
import net.skyedu.dao.UserInfoDao;


public class SubAppeal extends HttpServlet {
	private static final long serialVersionUID = 188152839809831083L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/xml;charset=UTF-8");
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		
		int attId = Integer.parseInt(request.getParameter("attId"));
		int appObject = Integer.parseInt(request.getParameter("appObject"));//申诉对象的teaId
		String area = request.getParameter("area");
		String uName = request.getParameter("uName");
		area=URLDecoder.decode(area,"UTF-8");
		uName=URLDecoder.decode(uName,"UTF-8");
		
		StringBuffer jsonAttr = new StringBuffer("[{");
		UserInfoDao ud = new UserInfoDao();
		 if(!ud.findAttId(attId)){  //如果申诉表里没有该条记录，就可以申诉否则不可申诉
			 Attendance att = new Attendance();
		        att.setAttid(attId);
		        att.setAttconfirm(2);
		        ud.updateConfirm(att);
		        Appeal app = new Appeal();
		        app.setSendname(uName);
		        app.setAppattid(attId);
		        app.setContent(area);
		        app.setRecipientid(appObject);
		        int num = ud.addAppeal(app);
		        
				if(num > 0){
					jsonAttr.append("\"a\":\""+1+"\"");
				}else{
					jsonAttr.append("\"a\":\""+0+"\"");
				}
		 }else{
			 jsonAttr.append("\"a\":\""+2+"\"");
		 }
		 jsonAttr.append("}]");
		 PrintWriter out = response.getWriter();
		 out.print(jsonAttr);
		 out.flush();
		 out.close();
	        
	}
}
